package com.ccp.dev.im.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.page.PageBean;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.im.model.ImSystemMessage;
import com.ccp.dev.im.model.ImUser;
import com.ccp.dev.im.service.ImSystemMessageService;
import com.ccp.dev.im.service.ImUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统消息 信息操作处理控制器
 * 
 * @author y
 * @date 2020-03-11 10:21:05
 */
@Controller
/**
 * 请将此名称添加到日志开关
 */
@Action(customModel = "系统消息")
@RequestMapping("/im/imSystemMessage")
public class ImSystemMessageController extends SysBaseController{
	@Resource
	private ImSystemMessageService imSystemMessageService;
	@Resource
	private ImUserService imUserService;

    /**
     * 功能描述: 跳转到首页
     *
     * @return: java.lang.String
     * @auther: y
     * @date: 2020-03-11 10:21:05
     */
    @GetMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description="跳转到系统消息首页", exectype = "页面跳转日志")
    public String viewList(HttpServletResponse response,HttpServletRequest request,Model model) {
        model.addAttribute("ctx",request.getContextPath());
        return "/functionPage/msgbox";
    }

    /**
    * 功能描述: 附带检索条件的分页查询
    *
    * @param: [request] http请求
    * @return: java.lang.Object
    * @auther: y
    * @date: 2020-03-11 10:21:05
    */
    @GetMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @ResponseBody
    @Action(description="附带检索条件的分页查询系统消息",detail="查询数据信息<#if success>成功<#else></#if>")
    public Object list(HttpServletRequest request) {
        QueryFilter queryFilter = new QueryFilter(request, true);
        ImUser imUser = imUserService.getByUserId(ContextUtil.getCurrentUserId());
        queryFilter.addFilter("receivUser",imUser.getId());
        //分页数据
        List<ImSystemMessage> pageDatas = imSystemMessageService.queryByReceiveUser(queryFilter);
        SysAuditThreadLocalHolder.putParamerter("success",true);
        PageBean pageBean = queryFilter.getPageBean();
        return success(pageDatas, pageBean.getLastPage());
    }


    /**
     * 功能描述: 更新系统消息状态
     *
     * @return: java.lang.String
     * @auther: y
     * @date: 2020-03-11 10:21:05
     */
    @RequestMapping("/updateMsgStatus")
    @ResponseBody
    @Action(description="更新系统消息状态", exectype = "更新日志")
    public Object updateMsgStatus(HttpServletRequest request,ModelMap modelMap) {
        ImUser imUser = imUserService.getByUserId(ContextUtil.getCurrentUserId());
        Map<String,Object> map = new HashMap();
        map.put("receivUser",imUser.getId());
        map.put("isRead", ImSystemMessage.IS_READ_Y);
        imSystemMessageService.updateMsgReadStatus(map);
        return getResultDataSuccess("operation.success");
    }

    /**
    * 功能描述: 根据主键ID删除数据的通用删除方法
    *
    * @param: id 参数名称必须是"id"
    * @return: java.lang.Object
    * @auther: y
    * @date: 2020-03-11 10:21:05
    */
    @PostMapping(UrlConstants.MODULE_FUNCTION_DELETE)
    @ResponseBody
    @Action(description="根据主键ID删除系统消息数据",exectype = "删除日志",detail="删除数据信息<#if success>成功<#else>失败</#if>")
    public Object delete(@RequestParam("id") String id) {
        if (StringUtil.isEmpty(id)) {
            SysAuditThreadLocalHolder.putParamerter("success",false);
            return fail("delete.failure.message");
        }
        int result = imSystemMessageService.delByIdArray(id.split(","));
        if(result > 0){
            SysAuditThreadLocalHolder.putParamerter("success",true);
        }else{
            SysAuditThreadLocalHolder.putParamerter("success",false);
        }
        return result > 0 ? getResultDataSuccess("delete.success") : getResultDataError("delete.failure");
    }



}
